<?php
/**
 * 国生今天值得买数据
 */
class GuoCommand extends CConsoleCommand 
{
    public function actionIndex()
    {
        $cate = Cat::getAllCat();
        $cateArray = array();
        foreach ($cate as $key => $val) {
            if ($val['id'] == '7') {
                $cateArray['男鞋女鞋'] = $val['id'];
            } elseif ($val['id'] == '4') {
                $cateArray['男士'] = $val['id'];
            } elseif ($val['id'] == '10') {
                $cateArray['电器'] = $val['id'];
            } elseif ($val['id'] == '9') {
                $cateArray['食品'] = $val['id'];
            } elseif ($val['id'] == '7' || $val['id'] == '8') {
                $cateArray['包包配饰'] = $val['id'];
            } else {
                $cateArray[$val['name']] = $val['id'];
            }
        }

        include_once __DIR__ . '/../../common/extensions/simple_html_dom.php';
        $html = file_get_html('http://link.51cuxiao.com/shikuaiqian_shuju.asp');
        $p = $html->find('p');
        foreach ($p as $element) {
            if (!isset($cateArray[$element->childNodes(7)->plaintext])) {
                continue;
            }
            $item = array();
            $item['title'] = $element->childNodes(1)->plaintext;
            $item['price'] = $element->childNodes(2)->plaintext;
            $item['pic_url'] = $element->childNodes(3)->plaintext;
            $item['origin_price'] = $element->childNodes(5)->plaintext;
            $item['cat_id'] = $cateArray[$element->childNodes(7)->plaintext];
            $item['created_at'] = $element->childNodes(8)->plaintext;
            $item['tbId'] = $element->childNodes(9)->plaintext;
            $item['url'] = $element->childNodes(10)->plaintext;
            $item['startTime'] = strtotime($element->childNodes(11)->plaintext);
            $item['endTime'] = strtotime($element->childNodes(12)->plaintext);

			$goods = Goods::model()->find(array(
				'select' => 'tb_id, end_time',
				'condition' => 'tb_id =:tb_id',
				'params' => array(':tb_id' => $item['tbId'])
			));
			if (empty($goods)) {
				$goods = new Goods();
			}
            Zhe::addGoods($goods, $item);
            echo "商品ID：{$item['tbId']}已被更新\n";
        }
    }

}
